﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using Character;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;

// Token: 0x02000166 RID: 358
public class CardFileList : MonoBehaviour
{
	// Token: 0x06000959 RID: 2393 RVA: 0x00036FA8 File Offset: 0x000351A8
	public void Seup(CardFileList.TYPE type, SEX sex)
	{
		this.path = this.directory + ((sex != SEX.FEMALE) ? "/Male/" : "/Female/");
		string[] files = Directory.GetFiles(this.path, "*.png", SearchOption.TopDirectoryOnly);
		for (int i = 0; i < files.Length; i++)
		{
			this.datas.Add(new Card_Data(files[i], this.noLoadTex));
		}
		this.previewImage.transform.parent.gameObject.SetActive(false);
		this.button_save_new.interactable = true;
		this.button_save_overwrite.interactable = false;
		this.button_load.interactable = false;
		this.button_delete.interactable = false;
		for (int j = 0; j < this.datas.Count; j++)
		{
			Toggle toggle = UnityEngine.Object.Instantiate<Toggle>(this.item_original);
			toggle.gameObject.SetActive(true);
			RectTransform rectTransform = toggle.transform as RectTransform;
			toggle.transform.SetParent(this.contentArea, false);
			toggle.group = this.group;
			toggle.isOn = false;
			this.toggles.Add(toggle);
		}
		this.select = -1;
		this.dataType = type;
		if (this.dataType == CardFileList.TYPE.CHARA)
		{
			this.sortType = GlobalData.sortChara;
		}
		else if (this.dataType == CardFileList.TYPE.COORDINATE)
		{
			this.sortType = GlobalData.sortCoord;
		}
		this.Sort_WithoutSE(this.sortType);
		this.sortUI.value = this.sortType;
		this.sortUI.onValueChanged.AddListener(new UnityAction<int>(this.Sort));
		this.ListPlacement();
	}

	// Token: 0x0600095A RID: 2394 RVA: 0x00037158 File Offset: 0x00035358
	private void Update()
	{
		float y = this.maskArea.sizeDelta.y;
		float y2 = this.contentArea.sizeDelta.y;
		float num = y2 - y;
		float value = 1f;
		if (y2 > 0f)
		{
			value = y / y2;
		}
		this.scroll.size = Mathf.Clamp(value, 0.2f, 1f);
		Vector2 anchoredPosition = this.contentArea.anchoredPosition;
		anchoredPosition.y = num * this.scroll.value;
		this.contentArea.anchoredPosition = anchoredPosition;
		int num2 = -1;
		for (int i = 0; i < this.toggles.Count; i++)
		{
			RectTransform rectTransform = this.toggles[i].transform as RectTransform;
			float y3 = rectTransform.anchoredPosition.y;
			float y4 = rectTransform.sizeDelta.y;
			float num3 = -anchoredPosition.y;
			float num4 = num3 - y;
			float num5 = y3;
			float num6 = y3 - y4;
			if (num6 <= num3 && num5 >= num4)
			{
				this.datas[i].show = true;
			}
			else
			{
				this.datas[i].show = false;
			}
			this.datas[i].isSelect = this.toggles[i].isOn;
			this.datas[i].UpdateSprite(this.toggles[i].image);
			if (this.toggles[i].isOn)
			{
				num2 = i;
			}
		}
		if (num2 == -1 && this.select >= this.datas.Count)
		{
			num2 = this.datas.Count - 1;
		}
		if (num2 != this.select)
		{
			SystemSE.Play(SystemSE.SE.CHOICE);
			this.select = num2;
			this.ChangeSelect();
		}
	}

	// Token: 0x0600095B RID: 2395 RVA: 0x00037360 File Offset: 0x00035560
	private void ChangeSelect()
	{
		if (this.select != -1 && !this.toggles[this.select].isOn)
		{
			this.toggles[this.select].isOn = true;
		}
		this.previewImage.transform.parent.gameObject.SetActive(this.select != -1);
		if (this.select != -1)
		{
			Texture2D texture2D = this.datas[this.select].Texture(true);
			Vector2 png_Size = CardFileList.PNG_Size;
			if (texture2D != null)
			{
				png_Size.x = (float)texture2D.width;
				png_Size.y = (float)texture2D.height;
			}
			this.previewImage.sprite = Sprite.Create(texture2D, new Rect(Vector2.zero, png_Size), png_Size * 0.5f, 100f, 0U, SpriteMeshType.FullRect);
			this.previewName.text = this.datas[this.select].charaInfo.name;
		}
		this.button_save_overwrite.interactable = (this.select != -1);
		this.button_load.interactable = (this.select != -1);
		this.button_delete.interactable = (this.select != -1);
	}

	// Token: 0x0600095C RID: 2396 RVA: 0x000374BC File Offset: 0x000356BC
	public void ScrollEvent(BaseEventData data)
	{
		if (this.yNum == 0)
		{
			return;
		}
		float num = 1f / (float)this.yNum * this.scrollSpeed;
		float num2 = -Input.mouseScrollDelta.y * num;
		this.scroll.value += num2;
	}

	// Token: 0x0600095D RID: 2397 RVA: 0x00037510 File Offset: 0x00035710
	public void Load()
	{
		if (this.select != -1)
		{
			SystemSE.Play(SystemSE.SE.CHOICE);
			this.loadUI.Setup(this.previewImage.sprite, this.datas[this.select].charaInfo.name, this.datas[this.select].file, new Action<int>(this.LoadExe));
		}
	}

	// Token: 0x0600095E RID: 2398 RVA: 0x00037584 File Offset: 0x00035784
	private void LoadExe(int filter)
	{
		this.onLoad.Invoke(this.datas[this.select].file, filter);
	}

	// Token: 0x0600095F RID: 2399 RVA: 0x000375A8 File Offset: 0x000357A8
	public void SaveNew()
	{
		SystemSE.Play(SystemSE.SE.CHOICE);
		this.onSave.Invoke(string.Empty);
	}

	// Token: 0x06000960 RID: 2400 RVA: 0x000375C0 File Offset: 0x000357C0
	public void SaveOverwrite()
	{
		if (this.select != -1)
		{
			SystemSE.Play(SystemSE.SE.CHOICE);
			this.onSave.Invoke(this.datas[this.select].file);
		}
	}

	// Token: 0x06000961 RID: 2401 RVA: 0x000375F8 File Offset: 0x000357F8
	public void Delete()
	{
		if (this.select != -1)
		{
			SystemSE.Play(SystemSE.SE.CHOICE);
			GameControl gameControl = UnityEngine.Object.FindObjectOfType<GameControl>();
			string text = this.datas[this.select].charaInfo.name + "を削除してもよろしいですか？";
			gameControl.CreateModalYesNoUI(text, new Action(this.DeleteExe), null);
		}
	}

	// Token: 0x06000962 RID: 2402 RVA: 0x00037658 File Offset: 0x00035858
	private void DeleteExe()
	{
		if (this.select == -1)
		{
			return;
		}
		File.Delete(this.datas[this.select].file);
		this.datas[this.select].Delete();
		UnityEngine.Object.Destroy(this.toggles[this.select].gameObject);
		this.datas.RemoveAt(this.select);
		this.toggles.RemoveAt(this.select);
		this.ListPlacement();
		if (this.select >= this.datas.Count)
		{
			this.select = this.datas.Count - 1;
		}
		this.ChangeSelect();
	}

	// Token: 0x06000963 RID: 2403 RVA: 0x00037718 File Offset: 0x00035918
	public void Sort(int type)
	{
		this.sortType = type;
		if (this.dataType == CardFileList.TYPE.CHARA)
		{
			GlobalData.sortChara = type;
		}
		else
		{
			GlobalData.sortCoord = type;
		}
		SystemSE.Play(SystemSE.SE.CHOICE);
		this.Sort_WithoutSE(type);
	}

	// Token: 0x06000964 RID: 2404 RVA: 0x0003774C File Offset: 0x0003594C
	public void Sort_WithoutSE(int type)
	{
		if (type == 0)
		{
			List<Card_Data> list = this.datas;
			if (CardFileList.callback0 == null)
			{
				CardFileList.callback0 = new Comparison<Card_Data>(CardFileList.Sort_Date);
			}
			list.Sort(CardFileList.callback0);
		}
		else if (type == 1)
		{
			List<Card_Data> list2 = this.datas;
			if (CardFileList.callback1 == null)
			{
				CardFileList.callback1 = new Comparison<Card_Data>(CardFileList.Sort_Date);
			}
			list2.Sort(CardFileList.callback1);
			this.datas.Reverse();
		}
		else if (type == 2)
		{
			List<Card_Data> list3 = this.datas;
			if (CardFileList.callback2 == null)
			{
				CardFileList.callback2 = new Comparison<Card_Data>(CardFileList.Sort_Name);
			}
			list3.Sort(CardFileList.callback2);
		}
		else if (type == 3)
		{
			List<Card_Data> list4 = this.datas;
			if (CardFileList.callback3 == null)
			{
				CardFileList.callback3 = new Comparison<Card_Data>(CardFileList.Sort_Name);
			}
			list4.Sort(CardFileList.callback3);
			this.datas.Reverse();
		}
		this.ListPlacement();
		this.ChangeSelect();
	}

	// Token: 0x06000965 RID: 2405 RVA: 0x00037848 File Offset: 0x00035A48
	private static int Sort_Name(Card_Data a, Card_Data b)
	{
		return string.Compare(a.charaInfo.name, b.charaInfo.name, false);
	}

	// Token: 0x06000966 RID: 2406 RVA: 0x00037868 File Offset: 0x00035A68
	private static int Sort_Date(Card_Data a, Card_Data b)
	{
		return DateTime.Compare(a.charaInfo.time, b.charaInfo.time);
	}

	// Token: 0x06000967 RID: 2407 RVA: 0x00037888 File Offset: 0x00035A88
	private void ListPlacement()
	{
		float num = this.maskArea.sizeDelta.x / (float)this.h_num;
		float d = num / CardFileList.PNG_Size.x;
		this.itemSize = CardFileList.PNG_Size * d;
		this.yNum = this.datas.Count / this.h_num;
		if (this.datas.Count % this.h_num != 0)
		{
			this.yNum++;
		}
		Vector2 sizeDelta = this.contentArea.sizeDelta;
		sizeDelta.y = this.itemSize.y * (float)this.yNum;
		if (sizeDelta.y < this.maskArea.sizeDelta.y)
		{
			sizeDelta.y = this.maskArea.sizeDelta.y;
		}
		this.contentArea.sizeDelta = sizeDelta;
		float num2 = 0f;
		for (int i = 0; i < this.datas.Count; i++)
		{
			float x = this.itemSize.x * (float)(i % this.h_num);
			Toggle toggle = this.toggles[i];
			RectTransform rectTransform = toggle.transform as RectTransform;
			rectTransform.anchoredPosition = new Vector2(x, num2);
			rectTransform.localScale = Vector3.one;
			if (i % this.h_num == this.h_num - 1)
			{
				num2 -= this.itemSize.y;
			}
			Text componentInChildren = this.toggles[i].GetComponentInChildren<Text>();
			componentInChildren.text = this.datas[i].charaInfo.name;
			if (this.datas[i].HasTex)
			{
				this.datas[i].ChangeTex();
			}
		}
	}

	// Token: 0x06000968 RID: 2408 RVA: 0x00037A74 File Offset: 0x00035C74
	public void Replacement(bool noSelect)
	{
		for (int i = 0; i < this.datas.Count; i++)
		{
			this.datas[i].Delete();
		}
		this.datas.Clear();
		string[] files = Directory.GetFiles(this.path, "*.png", SearchOption.TopDirectoryOnly);
		for (int j = 0; j < files.Length; j++)
		{
			this.datas.Add(new Card_Data(files[j], this.noLoadTex));
		}
		for (int k = 0; k < this.toggles.Count; k++)
		{
			UnityEngine.Object.Destroy(this.toggles[k].gameObject);
		}
		this.toggles.Clear();
		for (int l = 0; l < this.datas.Count; l++)
		{
			Toggle toggle = UnityEngine.Object.Instantiate<Toggle>(this.item_original);
			toggle.gameObject.SetActive(true);
			RectTransform rectTransform = toggle.transform as RectTransform;
			toggle.transform.SetParent(this.contentArea);
			toggle.group = this.group;
			toggle.isOn = false;
			this.toggles.Add(toggle);
		}
		if (noSelect)
		{
			this.select = -1;
		}
		else if (this.select >= this.datas.Count)
		{
			this.select = this.datas.Count - 1;
		}
		this.Sort_WithoutSE(this.sortType);
		this.ListPlacement();
		this.ChangeSelect();
	}

	// Token: 0x0400091B RID: 2331
	public static Vector2 PNG_Size = new Vector2(252f, 352f);

	// Token: 0x0400091C RID: 2332
	public Scrollbar scroll;

	// Token: 0x0400091D RID: 2333
	public Toggle item_original;

	// Token: 0x0400091E RID: 2334
	public RectTransform maskArea;

	// Token: 0x0400091F RID: 2335
	public RectTransform contentArea;

	// Token: 0x04000920 RID: 2336
	public int h_num = 3;

	// Token: 0x04000921 RID: 2337
	public Texture2D noLoadTex;

	// Token: 0x04000922 RID: 2338
	public int cacheNum = 100;

	// Token: 0x04000923 RID: 2339
	public float scrollSpeed = 1f;

	// Token: 0x04000924 RID: 2340
	public ToggleGroup group;

	// Token: 0x04000925 RID: 2341
	public Image previewImage;

	// Token: 0x04000926 RID: 2342
	public Text previewName;

	// Token: 0x04000927 RID: 2343
	private List<Card_Data> datas = new List<Card_Data>();

	// Token: 0x04000928 RID: 2344
	private List<Toggle> toggles = new List<Toggle>();

	// Token: 0x04000929 RID: 2345
	private Vector2 itemSize;

	// Token: 0x0400092A RID: 2346
	private int yNum;

	// Token: 0x0400092B RID: 2347
	private int select = -1;

	// Token: 0x0400092C RID: 2348
	public Button button_save_new;

	// Token: 0x0400092D RID: 2349
	public Button button_save_overwrite;

	// Token: 0x0400092E RID: 2350
	public Button button_load;

	// Token: 0x0400092F RID: 2351
	public Button button_delete;

	// Token: 0x04000930 RID: 2352
	[SerializeField]
	private string directory;

	// Token: 0x04000931 RID: 2353
	private string path;

	// Token: 0x04000932 RID: 2354
	[SerializeField]
	private LoadUI loadUI;

	// Token: 0x04000933 RID: 2355
	[SerializeField]
	private Dropdown sortUI;

	// Token: 0x04000934 RID: 2356
	[SerializeField]
	private CardFileList.SaveEvent onSave;

	// Token: 0x04000935 RID: 2357
	[SerializeField]
	private CardFileList.LoadEvent onLoad;

	// Token: 0x04000936 RID: 2358
	private CardFileList.TYPE dataType;

	// Token: 0x04000937 RID: 2359
	private int sortType;

	// Token: 0x04000938 RID: 2360
	[CompilerGenerated]
	private static Comparison<Card_Data> callback0;

	// Token: 0x04000939 RID: 2361
	[CompilerGenerated]
	private static Comparison<Card_Data> callback1;

	// Token: 0x0400093A RID: 2362
	[CompilerGenerated]
	private static Comparison<Card_Data> callback2;

	// Token: 0x0400093B RID: 2363
	[CompilerGenerated]
	private static Comparison<Card_Data> callback3;

	// Token: 0x02000167 RID: 359
	public enum TYPE
	{
		// Token: 0x0400093D RID: 2365
		CHARA,
		// Token: 0x0400093E RID: 2366
		COORDINATE
	}

	// Token: 0x02000168 RID: 360
	[Serializable]
	public class SaveEvent : UnityEvent<string>
	{
	}

	// Token: 0x02000169 RID: 361
	[Serializable]
	public class LoadEvent : UnityEvent<string, int>
	{
	}
}
